1
Inheritance and the Evolution of State
AI035 Lesson 2
00:00

In Ruby, inheritance isn't just about sharing methods; it's about the evolution of state. When we create a subclass like KaraokeSong < Song, we establish an "is-a" relationship where the child inherits the parent's structure while refining its identity.

1. The Initialization Chain

The super keyword is the bridge between generations. Within initialize, calling super passes arguments up to the parent's constructor, ensuring base instance variables (@name, @artist) are set before the subclass adds its specific state (@lyrics).

class Song@name, @artistclass KaraokeSong@lyricssuper() calls parent

2. Method Augmentation

Overriding a method like to_s allows us to extend behavior. By calling super inside the new definition, we capture the parent's string output and simply append the subclass's new data, maintaining a clean evolutionary path.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>